package com.ruoyi.puyu.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.puyu.domain.PuyuCoach;
import com.ruoyi.puyu.service.IPuyuCoachService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.puyu.domain.PuyuStrengthTest;
import com.ruoyi.puyu.service.IPuyuStrengthTestService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 体能测试Controller
 * 
 * @author wusongwen
 * @date 2024-05-01
 */
@RestController
@RequestMapping("/puyu/strengthtest")
public class PuyuStrengthTestController extends BaseController
{
    @Autowired
    private IPuyuStrengthTestService puyuStrengthTestService;
    @Autowired
    private IPuyuCoachService puyuCoachService;

    /**
     * 查询体能测试列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:strengthtest:list')")
    @GetMapping("/list")
    public TableDataInfo list(PuyuStrengthTest puyuStrengthTest)
    {
        puyuStrengthTest.setDelFlag("0");
        boolean bolRole = SecurityUtils.hasRole(UserConstants.ROLE_MANAGER)
                || SecurityUtils.hasRole(UserConstants.ROLE_ADMIN);
        // 教练只能查看自己的点名
        if (!bolRole) {
            PuyuCoach puyuCoach = puyuCoachService.selectPuyuCoachByUserId(SecurityUtils.getUserId());
            puyuStrengthTest.setCoachId(puyuCoach.getCoachId());
        }
        startPage();
        List<PuyuStrengthTest> list = puyuStrengthTestService.selectPuyuStrengthTestList(puyuStrengthTest);
        return getDataTable(list);
    }

    /**
     * 导出体能测试列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:strengthtest:export')")
    @Log(title = "体能测试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PuyuStrengthTest puyuStrengthTest)
    {
        List<PuyuStrengthTest> list = puyuStrengthTestService.selectPuyuStrengthTestList(puyuStrengthTest);
        ExcelUtil<PuyuStrengthTest> util = new ExcelUtil<PuyuStrengthTest>(PuyuStrengthTest.class);
        util.exportExcel(response, list, "体能测试数据");
    }

    /**
     * 获取体能测试详细信息
     */
    @PreAuthorize("@ss.hasPermi('puyu:strengthtest:query')")
    @GetMapping(value = "/{strengthTestId}")
    public AjaxResult getInfo(@PathVariable("strengthTestId") Long strengthTestId)
    {
        return success(puyuStrengthTestService.selectPuyuStrengthTestByStrengthTestId(strengthTestId));
    }

    /**
     * 新增体能测试
     */
    @PreAuthorize("@ss.hasPermi('puyu:strengthtest:add')")
    @Log(title = "体能测试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PuyuStrengthTest puyuStrengthTest)
    {
        return toAjax(puyuStrengthTestService.insertPuyuStrengthTest(puyuStrengthTest));
    }

    /**
     * 修改体能测试
     */
    @PreAuthorize("@ss.hasPermi('puyu:strengthtest:edit')")
    @Log(title = "体能测试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PuyuStrengthTest puyuStrengthTest)
    {
        return toAjax(puyuStrengthTestService.updatePuyuStrengthTest(puyuStrengthTest));
    }

    /**
     * 删除体能测试
     */
    @PreAuthorize("@ss.hasPermi('puyu:strengthtest:remove')")
    @Log(title = "体能测试", businessType = BusinessType.DELETE)
	@DeleteMapping("/{strengthTestIds}")
    public AjaxResult remove(@PathVariable Long[] strengthTestIds)
    {
        return toAjax(puyuStrengthTestService.deletePuyuStrengthTestByStrengthTestIds(strengthTestIds));
    }
}
